use std::time::{SystemTime, UNIX_EPOCH};

use state_machine::state_machine::StateMachine;

mod state_machine;
#[derive(Debug, PartialEq, Copy, Clone, Eq, Hash)]
enum State {
    // 支付创建
    PayCreate,
    // 支付中
    PayProcess,
    // 支付成功
    PaySuccess,
    // 支付失败
    PayFail,
}
#[derive(Debug, PartialEq, Copy, Clone, Eq, Hash)]
enum Event {
    // 创建完成
    PayCreated,
    // 支付中
    PayProcessing,
    // PaySuccess,
    // PayFailed,
}

/**
 * 状态机的简单实现
 */
fn main() {
    // 实例化
    let mut state_machine = StateMachine::new(State::PayCreate);

    // 添加逻辑
    state_machine.add_transition(State::PayCreate, Event::PayCreated, || State::PayProcess);
    state_machine.add_transition(State::PayProcess, Event::PayProcessing, || {
        // 模拟支付的两种情况
        let now = SystemTime::now();
        let seconds = now.duration_since(UNIX_EPOCH).unwrap().as_secs();
        if seconds % 2 == 0 {
            return State::PaySuccess;
        } 
        State::PayFail
    });

    // 使用状态机
    println!("当前状态为: {:?}", state_machine.get_current_state());
    state_machine.handle_event(Event::PayCreated);
    println!("当前状态为: {:?}", state_machine.get_current_state());
    state_machine.handle_event(Event::PayProcessing);
    println!("当前状态为: {:?}", state_machine.get_current_state());
}
